var MarketBookingTable = {
    id: "MarketBookingTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

MarketBookingTable.initColumn = function () {//初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='display: flex;align-items: center;'  ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            }
        },
        {title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return value === 1 ? '男' : value === 2 ? '女' : '未知';
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '电话号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 135},
        {title: '预约项目', field: 'appointmentPayService', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '跟踪状态', field: 'statuss', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '到店状态', field: 'status', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (value === "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {title: '预约医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '预约TC', field: 'tcName', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '预约门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<span title='" + value + "'>" + value + "</span>"
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
            }
        }
    ];
    return columns;
};

MarketBookingTable.search = function () {//查询预约客户列表
    MarketBookingTable.table.refresh({query: MarketBookingTable.formParams()});
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/reservationsNotArrive/getcount',
        data: MarketBookingTable.formParams(),
        dataType: "json",
        success: function (data) {
            $('#total').html("" + data + "");
        }
    });
};

MarketBookingTable.searchs = function () {
    document.getElementById("outpatientNameValue").value = $("#outpatientName").val();
    MarketBookingTable.search();
}

MarketBookingTable.formParams = function () {
    var queryData = {};
    queryData['reach'] = parent.$("#doctorTcInputPublic").val();  //姓名   电话   微信  预约号
    queryData['startAppointmentTime'] = $("#startAppointmentTime").val();
    queryData['endAppointmentTime'] = $("#endAppointmentTime").val();
    queryData['outpatientName'] = $("#outpatientNameValue").val();
    queryData['follower'] = $("#follower").val();
    queryData['reach'] = $("#reach").val();
    return queryData;
};

MarketBookingTable.resetEmpty = function () {//重置
    $("#startAppointmentTime").val("");
    $("#endAppointmentTime").val("");
    $("#outpatientName").val("");
    $("#outpatientNameValue").val("");
    $("#follower").val("");
    $("#reach").val("");
    $("#doctorTcInputPublic",parent.document).val("");
    MarketBookingTable.search();
};

MarketBookingTable.getFollower = function () {//选择跟进人
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

MarketBookingTable.today = function () {//今天 - 初始化赋值时间
    $("#startAppointmentTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endAppointmentTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    MarketBookingTable.search();
};

MarketBookingTable.yesterday = function () {//明天
    $("#startAppointmentTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
    $("#endAppointmentTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    MarketBookingTable.search();
};

$("#doctorTcSearchAll", parent.document).click(function(){//主页搜索
    MarketBookingTable.search();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#MarketBookingTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}

$(function () {
    var defaultColunms = MarketBookingTable.initColumn();
    var table = new BSTable(MarketBookingTable.id, "/reservationsNotArrive/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(MarketBookingTable.formParams());
    MarketBookingTable.table = table.init();
    MarketBookingTable.today();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {MarketBookingTable.search();});
    checkDate = function () {MarketBookingTable.search();};
    callbackTable = function () {MarketBookingTable.search();};
    phonePrivileges();
});